package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.AgeTag;
import cn.b.training.baby_education_training.entity.ClassifyTag;
import cn.b.training.baby_education_training.entity.FoodMenu;
import cn.b.training.baby_education_training.entity.SickTag;
import cn.b.training.baby_education_training.repository.AgeTagRepository;
import cn.b.training.baby_education_training.repository.ClassifyTagRepository;
import cn.b.training.baby_education_training.repository.FoodMenuRepository;
import cn.b.training.baby_education_training.repository.SickTagRepository;
import cn.b.training.baby_education_training.service.FoodMenuService;
import cn.b.training.baby_education_training.service.dto.FoodMenuDto;
import cn.b.training.baby_education_training.service.dto.MenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-03 15:32
 **/
@Service
public class FoodMenuServiceImpl implements FoodMenuService {
    @Autowired
    private AgeTagRepository ageTagRepository;
    @Autowired
    private SickTagRepository sickTagRepository;
    @Autowired
    private ClassifyTagRepository classifyTagRepository;
    @Autowired
    private FoodMenuRepository foodMenuRepository;
    @Autowired
    private BaiduAiService baiduAiService;
    @Override
    public boolean addFoodMenu(FoodMenuDto foodMenuDto) {
        AgeTag ageTag = ageTagRepository.findAgeTagById(foodMenuDto.getAgeTagId());
        SickTag sickTag = sickTagRepository.findSickTagById(foodMenuDto.getSickTagId());
        ClassifyTag classifyTag = classifyTagRepository.findClassifyTagById(foodMenuDto.getClassifyTagId());
        FoodMenu foodMenu = new FoodMenu();
        if (foodMenuDto.getFoodCover() != null) {
            String fileKey = UUID.randomUUID().toString();
            String fileName = foodMenuDto.getFoodCover().getOriginalFilename();
            if (fileName.indexOf(".") > 0) {
                String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
                fileKey += extName;
            }
            try {
                baiduAiService.putFile2(fileKey, foodMenuDto.getFoodCover().getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            foodMenu.setFoodCover("http://hzw-bos.bj.bcebos.com/food-menu/" + fileKey);
        }
        foodMenu.setAgeTagId(ageTag.getId());
        foodMenu.setSickTagId(sickTag.getId());
        foodMenu.setClassifyTagId(classifyTag.getId());
        foodMenu.setFoodTitle(foodMenuDto.getFoodTitle());
        foodMenu.setFoodText(foodMenuDto.getFoodText());
        foodMenu.setFoodDetail(foodMenuDto.getFoodDetail());
        foodMenu.setFoodVisit(foodMenuDto.getFoodVisit());
        foodMenu.setFoodLike(foodMenuDto.getFoodLike());
        foodMenu = foodMenuRepository.save(foodMenu);
        if (foodMenu!=null){
            return true;
        }
        return false;
    }

    @Override
    public MenuDto getFoodMenuById(Long id) {
        FoodMenu foodMenu = foodMenuRepository.getFoodMenuById(id);
        MenuDto dto = new MenuDto();
        dto.setId(foodMenu.getId());
        dto.setFoodTitle(foodMenu.getFoodTitle());
        dto.setFoodText(foodMenu.getFoodText());
        dto.setFoodDetail(foodMenu.getFoodDetail());
        dto.setFoodVisit(foodMenu.getFoodVisit());
        dto.setFoodLike(foodMenu.getFoodLike());
        dto.setFoodCover(foodMenu.getFoodCover());
        dto.setAgeTag(ageTagRepository.findAgeTag(foodMenu.getAgeTagId()));
        dto.setSickTag(sickTagRepository.findSickTag(foodMenu.getSickTagId()));
        dto.setClassifyTag(classifyTagRepository.findClassifyTag(foodMenu.getClassifyTagId()));
        return dto;
    }

    @Override
    public Boolean deleteFoodMenuById(Long id) {
        try{
            this.foodMenuRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<FoodMenu> getFoodMenuByTitle(String foodTitle) {
        return foodMenuRepository.getFoodMenuByTitle(foodTitle);
    }

    @Override
    public Page<FoodMenu> getAllMenus(Integer page, Integer size) {
        return this.foodMenuRepository.findAll(PageRequest.of(page,size));
    }
}
